/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.category;


import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import database.ProductDAO;
import database.ProductManagerDAO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import model.Category;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;



/**
 *
 * @author Trang
 */
@Namespace("/Administration/Catelogue")
public class WCategoryAction extends ActionSupport implements ModelDriven<Category>{
    private List<Category> ListCategories;
    private List<Category> ListCategoriesWithParent;
    private String keyword;
    private Category category = new Category();
    private Integer cid;
    public ProductManagerDAO productDAO = new ProductManagerDAO();
    public ProductDAO productDao = new ProductDAO();
    private List<String> categoriesName;
    private String yourCategoriesName;

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    @Override
    public Category getModel() {
        return category;
    }

    /**
     * @return the ListCategories
     */
    public List<Category> getListCategories() {
        return ListCategories;
    }

    /**
     * @param ListCategories the ListCategories to set
     */
    public void setListCategories(List<Category> ListCategories) {
        this.ListCategories = ListCategories;
    }

    /**
     * @return the category
     */
    public Category getCategory() {
        return category;
    }

    /**
     * @param category the category to set
     */
    public void setCategory(Category category) {
        this.category = category;
    }

    /**
     * @return the cid
     */
    public Integer getCid() {
        return cid;
    }

    /**
     * @param cid the cid to set
     */
    public void setCid(Integer cid) {
        this.cid = cid;
    }
    @Action(
            value="catelogue-management", results={
                @Result(name = "success", location ="/manageCategory/w_category.jsp")
            })
    public String CatelogueManagement()
    {
        Map session = ActionContext.getContext().getSession();
                session.put("category", 1);
                
        
        ListCategories = productDAO.getParentCategory();
        ListCategoriesWithParent = productDAO.getChildrenCategory();
        for(Category item : ListCategoriesWithParent)
        {
            ListCategories.add(item);
        } 
        String [] parent = new String[ListCategories.size()];
        int index = 0;
//        for(Category item : ListCategories)
//        {
//            parent[index] = item.getParentCategory().getName();
//            System.out.println(parent[index]);
//            index++;
//        }
//        session.put("categories", parent);
        return SUCCESS;
    }
    @Action(
            value="searchCategory", results={
                @Result(name = "success", location ="/manageCategory/w_category.jsp")
            })
    public String SearchCat()
    {
        Map session = ActionContext.getContext().getSession();
                session.put("category", 2);
        ListCategories = productDAO.searchForCategoryByKeyword(keyword);
        ListCategoriesWithParent = productDAO.getChildrenCategory();
         for(Category item : ListCategoriesWithParent)
        {
            ListCategories.add(item);
        } 
        return SUCCESS;
    }
    public List<Category> getListCategoriesWithParent() {
        return ListCategoriesWithParent;
    }

    /**
     * @param ListCategoriesWithParent the ListCategoriesWithParent to set
     */
    public void setListCategoriesWithParent(List<Category> ListCategoriesWithParent) {
        this.ListCategoriesWithParent = ListCategoriesWithParent;
    }
    
    
    @Action(value="add-category",
             results = {
                 @Result(name="success", location="/manageCategory/w_addcategory.jsp")
             })
     public String DisplayAddCategory()
     {
        categoriesName = new ArrayList<String>();
        categoriesName.add("No parent");
        ListCategories = productDAO.getParentCategory();
        for(Category item: ListCategories)
        {
            categoriesName.add(item.getName());
        }
        return SUCCESS;
     }
    
    @Action(value = "addcategory", results = {
        @Result(name = "success", location = "/manageCategory/w_category.jsp"),
        @Result(name = "input", location = "/manageCategory/w_addcategory.jsp")
    })
    @Override
    public String execute() throws Exception {
        String result;
        System.out.println("CategoryName: "+yourCategoriesName);
        if("No parent".equals(yourCategoriesName))
        {
            productDAO.addParentCategory(category);
            addActionMessage("Add Category Successfully!");
            ListCategories = productDAO.getParentCategory();
            ListCategoriesWithParent = productDAO.getChildrenCategory();
            result = SUCCESS;           
        }
        else
        {
            Category parent = new Category();
            parent =  productDAO.getCategoryFromName(yourCategoriesName);
            category.setParentCategory(parent);
            
            productDAO.addChildCategory(category);
            addActionMessage("Add Category Successfully!");
            ListCategories = productDAO.getParentCategory();
            ListCategoriesWithParent = productDAO.getChildrenCategory();
            result = SUCCESS;
        }


        return result;
    }

    /**
     * @return the categoriesName
     */
    public List<String> getCategoriesName() {
        return categoriesName;
    }

    /**
     * @param categoriesName the categoriesName to set
     */
    public void setCategoriesName(List<String> categoriesName) {
        this.categoriesName = categoriesName;
    }

    /**
     * @return the yourCategoriesName
     */
    public String getYourCategoriesName() {
        return yourCategoriesName;
    }

    /**
     * @param yourCategoriesName the yourCategoriesName to set
     */
    public void setYourCategoriesName(String yourCategoriesName) {
        this.yourCategoriesName = yourCategoriesName;
    }
    
}
